package com.springcloud.hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.springcloud.hystrix.service.HystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("/hystrix")
@RestController
@Slf4j
//@DefaultProperties(defaultFallback = "paymentInfo_Global_FallbackMethod")
public class HystrixController {

    @Resource
    public HystrixService hystrixService;

    @RequestMapping(value = "/payment/ok/{id}")
    //@HystrixCommand
    String paymentInfo_OK(@PathVariable Integer id) {
        //int a = 10/0;
        String ok = hystrixService.paymentInfo_OK(id);
        log.info(ok);
        return ok;
    }

    @RequestMapping(value = "/payment/timeout/{id}")
    //@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandel",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")})
    String paymentInfo_TimeOut(@PathVariable Integer id) {
        //int a = 10/0;
        return hystrixService.paymentInfo_TimeOut(id);
    }

    String paymentInfo_TimeOutHandel(@PathVariable Integer id) {
        return "消费端80进行服务降级，请稍后再试！id:" + id;
    }

    public String paymentInfo_Global_FallbackMethod(){
        return "我是全局统一返回处理，请稍后再试！爱你呦。。。。。";
    }
}
